const tools = {};
import router from '../router'
import ElementUI from 'element-ui';

// 复制
import Vue from 'vue'
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)


// 页面跳转
tools.route = (name,query) => {
  router.push({
    name,
    query
  })
}

//保留小数点
tools.number = (value,type) => {
  if(value==null || value==''){
      value=0;
  }
  //处理科学计数法
  value=value.toFixed(8);
  var arr=(value+"").split(".");
  var a1=arr[0];
  var a2=0;
  if(arr.length>1){
      a2=arr[1];
      if(a2.length>type){
          a2=a2.substring(0,type);
      }
  }
  value= Number(a1+"."+a2);
  return Number(value).toFixed(type)
}

tools.fileUrl = (file) => {
  var url;
  var agent = navigator.userAgent;
  if (agent.indexOf("MSIE") >= 1 || agent.indexOf("NET")!=-1) {
      url = window.URL.createObjectURL(file);
  } else if (agent.indexOf("Firefox") > 0) {
      url = window.URL.createObjectURL(file);
  } else if (agent.indexOf("Chrome") > 0) {
      url =window.webkitURL.createObjectURL(file);
  }
  return url;
}

tools.dateToString = (date) => { 
  var  year = date.getFullYear(); 
  var  month =(date.getMonth() + 1).toString(); 
  var  day = (date.getDate()).toString();  
  if  (month.length == 1) { 
      month =  "0"  + month; 
  } 
  if  (day.length == 1) { 
      day =  "0"  + day; 
  }
  var  dateTime = year +  "-"  + month +  "-"  + day;
  return  dateTime; 
},

tools.numTotime = (val) =>{
  var m = Math.floor(val/60) 
  var s = val%60
  m = m<10? "0"+ m : m
  s = s<10? "0"+ s : s
  return m + ":" + s
}

// 复制
tools.copy = (value) => {
  if(!value||value==undefined) return ElementUI.Message.error("复制内容为空")
  Vue.prototype.$copyText(value).then(
    res=>{
      console.log(res)
      ElementUI.Message.success("复制成功")
    },
    err=>{
      console.log(err)
      ElementUI.Message.error("复制失败")
    }
  )
}

// 返回
tools.back = () => {
  router.go(-1)
}

// 成功提示
tools.success = (value) => {
  ElementUI.Message.success(value)
}

// 失败提示
tools.warning = (value) => {
  ElementUI.Message.warning(value)
}

// 信息提示
tools.info = (value) => {
  ElementUI.Message.info(value)
}

// 错误提示
tools.error = (value) => {
  ElementUI.Message.error(value)
}

export default tools